var clientWidth,clientHeight;
var fuwuzhanId;

function upload(){
	var url = dewei_server+"/file/upload.do";
	window.webui.upload(url,"mendian","png;jpg","setShopLogo");
}
function setShopLogo(url){
	$("#model-imageUrl").val(url);
	$("#logo").attr("src", url);
}
$(function(){
	clientWidth=document.body.clientWidth;
	clientHeight=document.body.clientHeight;
	$('#aside_nav ul li a').click(function(){
			var $this=$(this);
			var event=$this.attr('event');
			if(event){
				$('#aside_nav ul li a.active').removeClass('active');
				$this.addClass('active');
			}else{
				var subs=$this.parent().find('.sub');
				if(subs.size()>0){
					var sub=$(subs.get(0));
					if(sub.css('display')=='none'){
						sub.toggle("blind");
						$this.find('.right').removeClass("fa-angle-right").addClass("fa-angle-down");
					}else{
						sub.toggle("blind");
						$this.find('.right').removeClass("fa-angle-down").addClass("fa-angle-right");
					}
				}
				return;
			}
			if($this.hasClass('login')){
				if(!fuwuzhanId){
					showLoginPage();
					return;
				}
			}
			$('#loading').show();
			eval(event);
		});
});

function showLoginPage(){
	$.get("login.json",function(html){
		$("#main_content").html(html);
		var username=$.cookie("username");
		$("#login_name").val(username);
	});
}

/**
* 跳转-密码修改
*/
function showPwdUpdate(){
	if(!fuwuzhanId){
		$('#aside_nav ul li a.active').removeClass('active');
		showLoginPage();
		return;
	}
	var url = "toUpdatePwd.json";
	$.get(url,function(html){
		$("#main_content").html(html);
		$('#loading').hide();
	});
}

/**
* 刷新界面
*/
function refresh(){
	$('#aside_nav ul li a.active').first().click();
}

/**
* 跳转-订单列表
*/
function showOrderList(status, page, title){
	$.post(
		"orderList.json?dt=" + new Date().getTime(),
		{status:status, page:page, fuwuzhanId:fuwuzhanId, title:title},function(html){
		$("#main_content").html(html);
		$('.main_table tr td .btn').click(function(){
			var data=$(this).attr('data');
			showOrderDetail(data);
		});
		$(".pagination li").click(function(){
			showOrderList(status, $(this).attr("page"), title);
		});
		$('#loading').hide();
	});
}

/**
* 跳转-门店列表
*/
function showMendianList(page){
	var url = "mendianList.json?fuwuzhanId="+fuwuzhanId+"&page="+page;
	$.get(url,function(html){
		$("#main_content").html(html);
		$('.main_table tr td .btn').click(function(){
			$('#loading').show();
			var data=$(this).attr('data');
			showMendianEdit(data);
		});
		$(".pagination li").click(function(){
			showMendianList($(this).attr("page"));
		});
		$('#loading').hide();
	});
}

/**
* 跳转-门店编辑界面
*/
function showMendianEdit(data){
	if(data){
		var data=eval("("+data+")");
	}
	var url = "mendianEdit.json";
	$.get(url,function(html){
		$('#loading').hide();
		$("#main_content").html(html);
		if(data){
			for(var name in data){
				var obj = $('#model-'+name);
				
				if(obj){
					obj.val(data[name]);
					if(name=='imageUrl'){
						if(data[name]){
							$("#logo").attr("src", data[name]);
						}
					}else if(name=='wxid'){
						$("#show-name").val(data[name]);
						$("#oldWxid").val(data[name]);
					}
				}
			}
		}else{
			$('#model-fuwuzhanId').val(fuwuzhanId);
		}

	});
}

/**
* 跳转-验证礼品券界面
*/
function showValidate(){
	var url = "validateCard.json";
	$.get(url,function(html){
		$("#main_content").html(html);
		$('#loading').hide();
	});
}

/**
* 登录确认
*/
function loginSubmit(){
	var loginName = $("#login_name").val();
	var password = $("#login_password").val();
	$.cookie("username",loginName);
	if(!loginName || !password){
		alert("请输入用户名或密码");
		return;
	}
	$('#loading').show();
	$.ajax({
		url: "../pad/loginSubmit.json",
		dataType:"json",
		data:{
			loginName: loginName,
			password: password
		},
		type:"post",
		success:function(data){
			$('#loading').hide();
			if(data.success){
				var info = data.fuwuzhan;
				$("#user_id").val(info.id);
				$("#user_account").val(loginName);
				$("#user_name").html(info.name);
				$("#main_content").html("");
				fuwuzhanId = $("#user_id").val();
				
				var curActive = $('#aside_nav ul li a.active');
				
			  	if(curActive.length > 0){
					$('#loading').show();
					var event = curActive.first().attr('event');
					eval(event);
				}else{
					$("#main_content").html('<div style="text-align: center; height: 100%;padding: 260px;font-size: 30px;color:#ccc;">'
						+'欢迎使用!'
						+'</div>');
				}
			}else{
				alert(data.message);
			}
		},
        error: function (msg) {
			$('#loading').hide();
         	alert("访问失败");
        }
	});
}

/**
* 密码修改确认
*/
function pwdSubmit(){
	var userid = $("#user_account").val();
	var orgPwd = $("#org_name").val();
	var newPwd = $("#new_password").val();
	var confirmPwd = $("#confirm_password").val();
	if(!orgPwd || !newPwd || !confirmPwd){
		alert("信息输入不完整");
		return;
	}else if(confirmPwd != newPwd){
		alert("两次输入密码不一致");
		return;
	}
	$('#loading').show();
	$.ajax({
		url: "../pad/pwdSubmit.json?",
		dataType:"json",
		data:{
			userid: userid,
			orgPwd: orgPwd,
			newPwd: newPwd
		},
		type:"post",
		success:function(data){
			$('#loading').hide();
			if(data.success){
         		alert("修改成功");
				$("#org_name").val("");
				$("#new_password").val("");
				$("#confirm_password").val("");
			}else{
				alert(data.message);
			}
		},
        error: function (msg) {
			$('#loading').hide();
         	alert("请求失败");
        }
	});
}

/**
*查看订单详情
*/
function showOrderDetail(data){
	var data=eval("("+data+")");
	$('#loading').show();
	$.get("orderDetail.json",function(html){
		$("#main_content").html(html);
		
		for(var name in data){
			if(name=='id'){
				$('#order-'+name).val(data[name]);
			}else{
				if(name=='kehuName' 
					&& (data[name]=='' || data[name]=='undefined')){
					$('#order-'+name).html("匿名");
				}else{
					$('#order-'+name).html(data[name]);
				}
			}
		}
		var status = data['state'];
		var curMendianId = data['mendianId'];
		if(status==1){
			$('#order-state').html('完成');
		}else if(status==2){
			$('#order-state').html('已指派');
		}else if(status==3){
			$('#order-state').html('作废');
		}else if(status==4){
			$('#order-state').html('过期');
		}else if(status==5){
			$('#order-state').html('已核实');
		}else if(status==0){
			$('#order-state').html('新订单');
			$("#btn_cancel").show();
			if(curMendianId == ''){
				$("#map").show();
				$("#btn_assign").show();
				$.ajax({
					url: "getMakers.json?lat="+data.lat+"&lng="+data.lng,
					dataType:"json",
					success:function(data1){
						var list = data1.result;
						var html="";
						var html_tmp=$("#mendian_tmp").html();
						for(i=0; i<list.length;i++){
							var bean = list[i];
							html += html_tmp;
							if(bean.imageUrl){
								html = html.replace("[logo]",bean.imageUrl);
							}else{
								html = html.replace("[logo]","../img/nopicture_icon.png");
							}
							
							html = html.replace("[name]",bean.name);
							html = html.replace("[telephone]",bean.lxr+"："+bean.lxrTel);
							html = html.replace("[address]",bean.address);
							html = html.replace("[mendianId]",bean.id);
							html = html.replace("[orderId]",data["id"]);
						}
						$("#marks").html(html);
						$(".mendianCon").click(function(){
							$(".mendianCon").removeClass("select");
							$(this).addClass("select");
							var mendian = $(this).find(".mendian");
							var mendianId = $(this).find(".mendianId");
							var valHtml = $(mendian).html();
							valHtml="<div style='color:green;'>"+valHtml+"<div>";
							$("#sel_mendian").html(valHtml);
							$("#mendian-id").val($(mendianId).val());
						});
					}
				});
			}else{
				$("#btn_examin").show();
				$('#mendian-id').val(curMendianId);
			}
		}
		if(curMendianId != ''){
			$.ajax({
				url: "getMenDianInfo.json?id="+curMendianId,
				dataType:"json",
				success:function(resultData){
					if(resultData.success){
						var bean = resultData.model;
						if(bean!=null){
							var html="";
							html+='<p>'+bean.name+'</p>';
							html+='<p>'+bean.lxr+"："+bean.lxrTel+'</p>';
							html+='<p>'+bean.address+'</p>';
							$("#sel_mendian").html(html);
						}
					}
				}
			});
		}
	 	$('#loading').hide();
	});
}

function assign(){
	var orderId = $("#order-id").val();
	var mendianId = $("#mendian-id").val();
	if(mendianId==''){
		alert("请选择维修点门店");
		return;
	}
	if(confirm("确定将订单指派给该门店？")){
		$.ajax({
			url: "assign.json?orderId="+orderId+"&shopId="+mendianId,
			dataType:"json",
			success:function(data){
				if(data.success){
					alert("指派门店完成");
				}else{
					alert("操作失败");
				}
			}
		});
	}
}
function saveMendian(){
	$('#loading').show();
  	$('#mendianForm').ajaxSubmit({
		url:'saveMenDian.json',
		type:'post',
		dataType:'json',
		success:function(data){
			$('#loading').hide();
			if(data.success){
				$($('#aside_nav ul li a').get(9)).click();
			}else{
				alert("操作失败");
			}
		}
	});
}

function updateOrderStatus(status){
	var confirmMsg="是否确定该操作？";
	if(status==3){
		confirmMsg="作废该订单？";
	}else if(status==5){
		confirmMsg="确定审核通过？";
	}
	if(confirm(confirmMsg)){
		var orderId = $("#order-id").val();
		$.ajax({
			url: "updateOrderStatus.json?orderId="+orderId+"&status="+status,
			dataType:"json",
			success:function(data){
				if(data.success){
					alert("操作完成");
				}else{
					alert("操作失败");
				}
			}
		});
	}
}

function initMap(width,height,lat,lng){
	$('#map').css('width',width+"px");
	$('#map').css('height',height+"px");
      	var latlng =new google.maps.LatLng(lat,lng);
     		var options = {zoom:15,center:latlng,mapTypeId : google.maps.MapTypeId.ROADMAP};
          var map1 =new google.maps.Map(document.getElementById('map'),options);
          
         	var id=new Date().getTime();
         
          var html="<div style='font-size:14px;width:190px'><b>最近门店：南京高科电动车</b></div>";
	html+="<div style='font-size:12px;'>地址：三山街石头路23号</div>";
	html+="<div style='font-size:12px;'>电话：18588888888</div>";
	html+="<div style='font-size:12px;'>&nbsp;</div>";
	html+="<div style='font-size:12px;'><a class='btn blue-bg'>指定门店</a><a class='btn red-bg' style='float:right' id='cls"+id+"'>关闭窗口</a></div>";
	var latlng =new google.maps.LatLng(32.083722,118.881026);
	var marker1 =new google.maps.Marker({
                   position : latlng,
                   map : map1
          });
         
          var infowindow =new google.maps.InfoWindow({ content : html});
          
          google.maps.event.addListener(marker1, 'click', function () {
 		infowindow.open(map1, marker1);
	});
	
          infowindow.open(map1,marker1);// 打开窗口

	html="<div style='font-size:12px;'>客户位置</div>";
	var latlng =new google.maps.LatLng(lat,lng);
	var marker2 =new google.maps.Marker({
                   position : latlng,
                   map : map1
          });
           
        var   orderWindow =new google.maps.InfoWindow({ content : html});
          google.maps.event.addListener(marker2, 'click', function () {
 		orderWindow.open(map1, marker2);
	});
          orderWindow.open(map1,marker2);// 打开窗口
          map1.setCenter(latlng, 15); 
          
           $('#cls'+id).click(function(){
           	alert(32);
          	orderWindow.close();
          });
}
function back(){
	window.webui.back();
}
function toMap(){
	window.webui.toMap();
}
function setLocation(lat, lng){
	$("#model-latitude").val(lat);
	$("#model-longitude").val(lng);
	$.ajax({
		url: "geocoder.json?lat="+lat+"&lng="+lng,
		dataType:"json",
		success:function(data){
			var result = data.result;
			if(result){
				$("#model-address").val(result.formatted_address);
			}
		}
	});
 }
function searchCard(){
	var sn=$("#sn").val();
	$.ajax({
		url:"searchCard.json?sn="+sn,
		dateType:"html",
		success:function(html){
			 $("#search_result").html(html);
		}
	});
}